import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, PopoverController, ToastController} from 'ionic-angular';
import {ClientIndexProvider} from "../../../providers/client/client-index/client-index.provider"
import {SideMenuService} from "../../../services/common/side-menu.service";
import {TabsPage} from "../../tabs/tabs";
import {LoadingService} from "../../../services/common/loading.service";

@IonicPage()
@Component({
    selector: 'page-client-index',
    templateUrl: 'client-index.html',
    providers: [
        ClientIndexProvider,
        SideMenuService
    ]
})
export class ClientIndexPage {
    //个险数量
    personalInsurance: string = '';

    openRoot = (name, index) => this.openTab(name, index);

    openMenu(myEvent) {
        this.sideMenuService.openSideMenu(myEvent)
    }

    openTab(name, index) {
        this.navCtrl.popToRoot();
        this.tabsPage.openPage({name: name, index: index});
    }

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public cliIndPro: ClientIndexProvider,
                public toastCtrl: ToastController,
                private sideMenuService: SideMenuService,
                public tabsPage: TabsPage,
                private load: LoadingService,) {
        this.getPersonalNum();
    }

    ionViewDidLoad() {
    }

    ionViewDidEnter() {
    }

    /**
     * 获取个险客户数量
     */
    getPersonalNum(notLoading = 0) {
        // if (this.personalInsurance) return;
        this.load.showLoading();
        this.doGetPersonalNum(notLoading, ()=>{
            this.load.dismissLoading();
        });
    }

    doGetPersonalNum(notLoading, callback) {
        this.cliIndPro.getPersonalInsuranceNum(notLoading).subscribe(aa => {
            if (aa == null)
                aa = 0;
            this.personalInsurance = aa;
            if(callback){
                callback();
            }
        }, err => {
            if(callback){
                callback();
            }
        });
    }
    /**
     *  toast 提示信息
     * @param {string} message 提示内容
     */
    toast(message: string) {
        this.toastCtrl.create({
            message: message,
            position: 'middle',
            duration: 1200
        }).present();
    }

    /**
     * 下拉刷新
     */
    doRefresh(refresher) {
        this.doGetPersonalNum(1, ()=>{
            refresher.complete();
        });
    }

    /**
     * 跳转到生日客户列表
     */
    goToBirthCust() {
        this.navCtrl.push('ClientBirthPage');
    }

    /**
     * 跳转到续期客户列表
     */
    goToRenewalCust() {
        this.navCtrl.push('ClientRenewalPage');
    }

    /**
     * 跳转到理赔客户列表
     */
    goToCompenCust() {
        this.navCtrl.push('ClientCompensationPage');
    }

    /**
     * 跳转到准客户列表
     */
    goToProspCust() {
        this.navCtrl.push('ClientProspectivePage');
    }

    /**
     * 跳转到通讯录
     */
    goToClientList() {
        this.navCtrl.push('ClientListPage');
        // this.navCtrl.push('ClientContactPage');
        // this.navCtrl.push('ClientDirectoryPage');
    }

    /**
     * 跳转到VIP客户
     * * @params type:number
     */
    goToVIPClients() {
        this.navCtrl.push('ClientVipPage');
    }

    /**
     * 跳转到个险客户
     * @params type:number
     */
    goToPersonalClients() {
        this.navCtrl.push('ClientSinglePage', {
            personalInsurance: this.personalInsurance
        },{ duration: 0 });
    }

    /**
     * 跳转到添加准客户
     */
    goToAdd(){
        this.navCtrl.push('ClientsAddPage')
    }
    /**
     * 跳转到客户筛选
     */
    goToClientFilter() {
        this.navCtrl.push('ClientFilterPage');
    }

    /**
     * 跳转到客户地图
     */
    goToClientBMap() {
        this.navCtrl.push('ClientBMapPage', {origin: 'map-page'});
    }
}
